<?php
/**
**2015-09-24 芙蓉园切换两次交互模式
**User By  PZP
**/
include_once('/var/www/html/new/d/module/api/function.class.php');
include('/var/www/html/new/d/module/api/FuRongYuan/furongyuan.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$txt=json_encode($_REQUEST);
$PFT_infunction = new AllFunction();

logfile($txt,'fry_get_data');

//{"VenueID":"90FA3B1508DB4CD3B4D6D70BBEED5BDA","RoundID":"","AreaID":"","SeatID":"","Tnum":"1","Fid":"113","PayStatus":"1","TicketID":"21247","TouristName":"\u5f6d\u5fd7\u840d","TouristTel":"15959110796","PlayDate":"2015-08-26","IdCard":"350321199108230122"}

if(!isset($_REQUEST['Action'])){
	
	if(!$_REQUEST['TicketID']) exit("101|0|0|0|0|0");
	
	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	$select_uuid = "select uuid,landid from uu_jq_ticket where id=$tid limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('101');		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	
	$lid = $GLOBALS['le']->f('landid'); 

	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$submit_array = array(
		'uuid' 	 		=> $uuid,								// 门票编码
		'pftordernum' 	=> $ls_order,							// 临时订单号
		'UUtnum'		=> $_REQUEST['Tnum'],					// 数量
		'pftordername'  => $_REQUEST['TouristName'],			// 姓名
		'UUordertel'	=> $_REQUEST['TouristTel'],				//手机
		'IDcar'			=> $_REQUEST['IdCard'],					// 身份证
	);
	
	$fry = new FuRongYuan($lid);
	$SubmitOrder =$fry->SubmitOrder($submit_array);
	if($SubmitOrder != 101){
		if($_REQUEST['PayStatus'] == 1){
			$payOrder = $fry->PayOrder($SubmitOrder);	
			if($payOrder == 101) exit("101");
			else{				
				exit("200|0|0|0|".$SubmitOrder."|".$ls_order);//青岛没有订单号，将临时订单号作为UUtordernum值记录
			}
		}
	}else{
		exit("101");
	}
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功要发短信

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$ordernum = (string)$pft_order_xml->Rec->UUordernum;
	$lid = (string)$pft_order_xml->Rec->UUlid;
	$tid = (string)$pft_order_xml->Rec->UUtid;
	$aid = (string)$pft_order_xml->Rec->UUaid;
	$fid = (string)$pft_order_xml->Rec->UUbuyid;
	$pid = (string)$pft_order_xml->Rec->UUpid;
	$title = (string)$pft_order_xml->Rec->UUltitle.(string)$pft_order_xml->Rec->UUttitle;
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$begintime = (string)$pft_order_xml->Rec->UUbegintime;
	$vcode = (string)$pft_order_xml->Rec->UUvcode;
	$tordernum = (string)$pft_order_xml->Rec->UUtordernum;
	$tel = (string)$pft_order_xml->Rec->UUordertel;

	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$ordernum','$vcode','$tordernum',9,0,$tnum,'$vcode',0,now())"; //芙蓉园没有订单号，将apiOrder作为vcode值记录
	$GLOBALS['le']->query($str);
	
	// $sms = $PFT_infunction->HQ_send_sms($tel,$aid,$fid,$tid,$pid,$ordernum,$lid,$title,$tnum,$begintime,$vcode);
	// if($sms == 100)
	// $QD->logfile($ordernum.'|'.$vcode,'send_sms_success');	
}
elseif($_REQUEST['Action'] == 'Relation_after_pay'){  //散客支付订单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);		
	$vcode = (string)$pft_order_xml->Rec->vcode;
	$lid = (string)$pft_order_xml->Rec->UUlid;
	$fry = new FuRongYuan($lid);
	$fry->PayOrder($vcode);
}
elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	if($_REQUEST['Tnum'] != 0) exit("101");  //没有修改的接口,需要整笔退单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);		
	$UUtordernum = (string)$pft_order_xml->Rec->UUtordernum;
	$UUvcode = (string)$pft_order_xml->Rec->UUvcode;
	$UUpersonid = (string)$pft_order_xml->Rec->UUpersonid;
	$UUlid = (int)$pft_order_xml->Rec->UUlid;

	$refund_array = array(
		'APIOrdern'	=>$UUvcode,
		'idcard'	=>$UUpersonid
	);
	$fry = new FuRongYuan($UUlid);
	$RefundOrder =$fry->RefundOrder($refund_array);
	if($RefundOrder == 100) {
		$str="update all_api_order set oStatus=3,handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);
		exit("200|0|0|0|0|0");
	}
	else exit("1077");

}

// function logfile($txt,$logname){
	// $fp = fopen('/var/www/html/new/d/logs/api/FuRongYuan/'.date('Y-m-d').$logname.'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp); 
// }
function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>